package codeRandomThoughts.Test77组合;

import java.util.ArrayList;
import java.util.List;

public class Solution2 {
    List<Integer> path;
    List<List<Integer>> result;

    public List<List<Integer>> combine(int n, int k) {
        path = new ArrayList<>();
        result = new ArrayList<>();

        backtracking(n, k, 1);
        return result;
    }

    public void backtracking(int n, int k, int start) {
        //回溯终止条件
        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }

        //单层回溯的逻辑
        //i的最大值进行了剪枝
        for (int i = start; i <= (n - (k - path.size()) + 1); i++) {
            path.add(i);
            backtracking(n, k, i + 1);
            path.remove(path.size() - 1);
        }
    }
}
